'use strict';
/**
 * 登录服务
 */
const Result = require('../utils/result');
const Service = require('egg').Service;
class LoginService extends Service {

  /**
   * 登录验证
   * @params name 用户名
   * @password password 密码
   */
  async authLogin({userName,password}){
    const {ctx} = this;
    let admin = await ctx.model.Admin.findOne({ where: { name: userName}});
    if(admin){
      if(admin.password === password){
        let accesses = await ctx.model.RoleAccess.find({where:{role_id:admin.role_id}});
        let arr = accesses.access_id.split(',');
        return new Result(200, '登录成功', arr);
      }else{
        return new Result(201,'密码错误',null)
      }
    }else{
      return new Result(201,'用户名错误',null)
    }
  }
}

module.exports = LoginService;
